package com.miyake.loveviewweb.client.dialog;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.HTML;

public abstract class BasicDialog extends Composite implements HasText {

    public SimplePanel getContentPanel() {
        return contentPanel;
    }

    private static BasicDialogUiBinder uiBinder = GWT
        .create(BasicDialogUiBinder.class);

    interface BasicDialogUiBinder extends UiBinder<Widget, BasicDialog> {
    }

    
    public BasicDialog(IDialogPanel dialogPanel) {
        initWidget(uiBinder.createAndBindUi(this));
        this.composite = dialogPanel.getComposite();
        dialogPanel.setDialogHandler(new DialogHandler() {

            public void onOK() {
                BasicDialog.this.onOK(BasicDialog.this.composite);
                hideDialog();
            }

            public void onCancel() {
                BasicDialog.this.onCancel();
                hideDialog();
            }
            
        });
    }
    
    public BasicDialog(String html) {
         initWidget(uiBinder.createAndBindUi(this));
         this.title.setHTML(html);
    }
    
    @UiField Button buttonCancel;
    @UiField Button buttonOK;
    @UiField SimplePanel contentPanel;
    @UiField HTML title;

    @UiHandler("buttonCancel")
    void onClick(ClickEvent e) {
        onCancel();
        this.hideDialog();
    }

    protected void onCancel() {

    }
    abstract protected void onOK(Composite composite);

    public void setText(String text) {
        buttonCancel.setText(text);
    }

    public String getText() {
        return buttonCancel.getText();
    }

    @UiHandler("buttonOK")
    void onButtonOKClick(ClickEvent event) {
        onOK(composite);
        this.hideDialog();
    }
    
    

    private DialogBox dlg;
    private Composite composite;
    public void showDialog() {
        this.buttonOK.setVisible(isOkVisible());
        this.buttonCancel.setVisible(isCancelVisible());
        
        if (composite != null) {
            this.contentPanel.add(composite);
        }
        dlg = new DialogBox();
        dlg.add(this);
        dlg.center();
        dlg.setAnimationEnabled(true);
        dlg.setAutoHideEnabled(false);
        dlg.addCloseHandler(new CloseHandler<PopupPanel>() {

            public void onClose(CloseEvent<PopupPanel> event) {

            }
            
        });
    }
    
    protected boolean isCancelVisible() {
        return true;
    }

    protected boolean isOkVisible() {
        return true;
    }

    public void hideDialog() {
        if (dlg != null) {
            dlg.hide();
        }
    }
}
